package com.erp.erp_ui.Message;

import com.erp.erp_entitys.Message.UserUnReadGroupTypeRes;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Message.IMessageService;
import com.erp.erp_ui.Message.req.EditMessageReq;
import com.erp.erp_ui.Message.res.EditMessageRes;
import com.erp.erp_ui.Message.service.MessageAllService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @ClassName MessageControl
 * @Description 消息控制类
 * @Author songjizhen
 * @Date 2020-06-30 22:54
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "message")
public class MessageControl extends Base {
    private static final Logger logger = LoggerFactory.getLogger(MessageControl.class);
    @Autowired
    IMessageService messageService;
    @Autowired
    MessageAllService messageAllService;

    /**
     * 主页当前登录人所有创建消息
     * @return
     */
    @RequestMapping(value = "selectByPage")
    @ResponseBody
    public R  selectByPage(Integer type){
        try{
            UserEntity user = (UserEntity) this.getBean("user");
            PageInfo pageInfo = messageAllService.queryMessageByParam(type,user.getCode());
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 保存消息
     * @param req
     * @return
     */
    @RequestMapping(value = "saveMessage",method = RequestMethod.POST)
    @ResponseBody
    public R saveMessage(@RequestBody EditMessageReq req){
        try{
            logger.info("消息-新增-req="+req);
            UserEntity user = (UserEntity) this.getBean("user");
            R r = messageAllService.editMessage(req, user.getName(), user.getCode());
            return  r;
        }catch (Exception e){
            logger.info("消息-新增异常-e="+e.getMessage());
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 主页 批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = "delmessage",method = RequestMethod.POST)
    @ResponseBody
    public R delMessage(String[] ids){
        try{
            List<Integer> list=new ArrayList<>();
            for(int i=0;i<ids.length;i++){
                list.add(Integer.parseInt(ids[i].replace("[","").replace("]","")));
            }
            messageAllService.delMessageByIds(list);
            return R.build(ResultStatusEnum.SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping(value = "queryByid")
    @ResponseBody
    public  R queryByid(Integer id){
        try{
            EditMessageRes res = messageAllService.queryById(id);
            return R.build(res);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }




    /**
     * 未读消息提醒
     * @return
     */
    @RequestMapping(value = "showMessage" )
    @ResponseBody
    public R<Integer> showMessage(){
        try {
            UserEntity user = (UserEntity) this.getBean("user");
            Integer number = messageAllService.getUserUnReadNumber(user.getCode());
            return R.build(number);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     *  查询当前登录人 的未读消息，根据类型分类
     * @return
     */
    @RequestMapping(value = "queryUnReadByType")
    @ResponseBody
    public  R queryUnReadByType(){
        try{
            UserEntity user = (UserEntity) this.getBean("user");
            UserUnReadGroupTypeRes res = messageAllService.queryUnReadByType(user.getCode());
            return R.build(res);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }





}
